package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type JudgmentParagraphService struct{}

// CreateJudgmentParagraph 创建裁决结果管理记录
// Author [yourname](https://github.com/yourname)
func (JPGService *JudgmentParagraphService) CreateJudgmentParagraph(ctx context.Context, JPG *flaud.JudgmentParagraph) (err error) {
	err = global.GVA_DB.Create(JPG).Error
	return err
}

// DeleteJudgmentParagraph 删除裁决结果管理记录
// Author [yourname](https://github.com/yourname)
func (JPGService *JudgmentParagraphService) DeleteJudgmentParagraph(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.JudgmentParagraph{}, "id = ?", ID).Error
	return err
}

// DeleteJudgmentParagraphByIds 批量删除裁决结果管理记录
// Author [yourname](https://github.com/yourname)
func (JPGService *JudgmentParagraphService) DeleteJudgmentParagraphByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.JudgmentParagraph{}, "id in ?", IDs).Error
	return err
}

// UpdateJudgmentParagraph 更新裁决结果管理记录
// Author [yourname](https://github.com/yourname)
func (JPGService *JudgmentParagraphService) UpdateJudgmentParagraph(ctx context.Context, JPG flaud.JudgmentParagraph) (err error) {
	err = global.GVA_DB.Model(&flaud.JudgmentParagraph{}).Where("id = ?", JPG.ID).Updates(&JPG).Error
	return err
}

// GetJudgmentParagraph 根据ID获取裁决结果管理记录
// Author [yourname](https://github.com/yourname)
func (JPGService *JudgmentParagraphService) GetJudgmentParagraph(ctx context.Context, ID string) (JPG flaud.JudgmentParagraph, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&JPG).Error
	return
}

// GetJudgmentParagraphInfoList 分页获取裁决结果管理记录
// Author [yourname](https://github.com/yourname)
func (JPGService *JudgmentParagraphService) GetJudgmentParagraphInfoList(ctx context.Context, info flaudReq.JudgmentParagraphSearch) (list []flaud.JudgmentParagraph, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.JudgmentParagraph{})
	var JPGs []flaud.JudgmentParagraph
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["paragraph_order"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&JPGs).Error
	return JPGs, total, err
}
func (JPGService *JudgmentParagraphService) GetJudgmentParagraphPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
